package com.cmwa.solution.sys.dictionary.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.exception.ValidateFailedException;
import com.cmwa.solution.sys.dictionary.dao.DictionaryDao;
import com.cmwa.solution.sys.dictionary.entity.DictionaryVo;
import com.cmwa.solution.sys.dictionary.service.DictionaryService;
import com.cmwa.solution.utils.Constant;
import com.cmwa.solution.utils.SessionUtils;

@Service
public class DictionaryServiceImpl implements DictionaryService {

	@Resource
	private DictionaryDao dictionaryDao;

	@Override
	public List<DictionaryVo> listDictionary(DictionaryVo dictionaryVo) {
		return dictionaryDao.listDictionary(dictionaryVo);
	}

	@Override
	public List<DictionaryVo> dictionarys(DictionaryVo dictionaryVo) {
		return dictionaryDao.dictionarys(dictionaryVo);
	}
	
	/**
	 * 分页查询数据字典
	 */
	@Override
	public Page<DictionaryVo> dictionaryListPage(SearchParam param) {
		List<DictionaryVo> items = dictionaryDao.dictionaryListPage(param);
		return Page.create(items, param.getStart(), param.getLimit(), param.getTotal());
	}

	/**
	 * 新增系统数据字典
	 * @param dictionaryVo
	 */
	@Override
	public void saveDictionary(DictionaryVo dictionaryVo) throws ValidateFailedException {
		// 用于校验停用状态的数据字典
		dictionaryVo.setStat(Constant.C_STATUS_D);
		int count = dictionaryDao.checkDictionary(dictionaryVo);
		if (count > 0) {
			throw new ValidateFailedException("字典值["+ dictionaryVo.getDctValue() + "]已被使用");
		} else {
			// 获取新增新增数据字典初始状态
			dictionaryVo.setStat(Constant.C_STATUS_C);
			// 获取登陆ID
			dictionaryVo.setCreateId(SessionUtils.getEmployee().getID());
			dictionaryDao.addDictionary(dictionaryVo);
		}
	}

	/**
	 * 根据字典ID查询系统数据字典信息
	 * @param dctId
	 */
	 @Override
	 public DictionaryVo queryDictionaryByDctId(String dctId) throws ValidateFailedException {
		 if(null == dctId){
			 throw new ValidateFailedException("字典ID为空!");
		 }
		  return dictionaryDao.queryDictionaryByDctId(dctId); 
	 }

	 /**
 	 * 删除系统数据字典信息
 	 * @param dctId
 	 */
	 @Override 
	 public void deleteDictionary(String dctId) { 
         DictionaryVo dictionaryVo = new DictionaryVo(); 
         dictionaryVo.setDctId(dctId);
         dictionaryVo.setStat(Constant.C_STATUS_D);//修改 为停用状态
         dictionaryDao.deleteDictionary(dictionaryVo); 
	 }
	 
	 /**
	 * 修改数据字典信息
	 */
	 @Override 
	 public void updateDictionary(DictionaryVo dictionaryVo) throws ValidateFailedException { 
		 System.out.println(dictionaryVo.getDctId());
		 String oldDctValue = dictionaryDao.queryDctValueByDctId(dictionaryVo);
		 dictionaryVo.setOldDctValue(oldDctValue);
		 dictionaryVo.setStat(Constant.C_STATUS_D);
		 int count = dictionaryDao.checkDictionary(dictionaryVo);
		 if (count > 0) {
				throw new ValidateFailedException("字典值["+ dictionaryVo.getDctValue() + "]已被使用");
			} else {
				// 获取新增新增数据字典初始状态
				dictionaryVo.setStat(Constant.C_STATUS_C);
				// 获取登陆ID
				dictionaryVo.setCheckId(SessionUtils.getEmployee().getID());
				System.out.println(dictionaryVo.getDctId());
				dictionaryDao.updateDictionary(dictionaryVo);
			}
	 }
}
